<?php 

include MODULE_LIB_PATH . 'Category.class.php';

/**
 * 商品控制器
 */
class GoodsController extends CommonController{
	
	private $model;
	
	public function __init(){
		parent::__init();
		
	    $this->model = K('Goods');
	}
	
    /**
     *  商品首页
     */
    public function index(){
    		$gcid = Q('get.gcid', 0, 'intval');
    		$data = M()->join("__goods__ AS g JOIN __admin__ AS a ON g.admin_aid=a.aid")->where("goods_class_gcid={$gcid}")->all();
		foreach($data as $k=>$v){
			$data[$k]['stock'] = K('GoodsList')->where("goods_gid=" . $v['gid'])->sum('inventory');
		}
		
		$this->assign('data', $data);		
       	$this->display(); 
    }	
	
	// 添加商品
	public function add(){
		$gcid = Q('get.gcid', 0, 'intval');
		if (IS_POST){
			if (!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success('添加商品成功', U('Index', array('gcid'=>$gcid)));
		}
		$categoryData = K('Category')->all();
		$categoryData = Category::toLevel($categoryData, "----");
		$this->assign('categoryData', $categoryData)	;			
		$this->assign('brandData', K('Brand')->all());
	    $this->display();
	}
	
	// 编辑
	public function edit(){
		$gcid = Q('get.gcid', 0, 'intval');
		if (IS_POST){
			if (!$this->model->editData()){
				$this->error($this->model->error);
			}
			$this->success('修改商品成功', U('Index', array('gcid'=>$gcid)));
		}
		$gid = Q('get.gid', 0, 'intval');
		$oldData = M()->join("__goods__ AS g JOIN __goods_detail__ AS gd ON g.gid=gd.goods_gid")->where("gid={$gid}")->find();
		
		if ($oldData['albumpic']==''){
			$oldData['albumpic'] = array();
		}else{
			$oldData['albumpic'] = explode(',', $oldData['albumpic']);
		}
		
		$this->assign('oldData', $oldData);
				
		$categoryData = K('Category')->all();
		$categoryData = Category::toLevel($categoryData, "----");
		$this->assign('categoryData', $categoryData)	;	
		
		// 获取该分类的所有属性和规格
		$cid = $oldData['category_cid'];
		$gavalue = K('GoodsAttr')->where("goods_gid={$gid}")->all();
		$cateTypeAttrdata = M()->join("__category__ AS a JOIN __type_attr__ AS ta ON a.tid=ta.tid")->where("cid={$cid}")->all();
		
		$attrs = array();
		$specs = array();
		foreach($cateTypeAttrdata as $k=>$v){
			$cateTypeAttrdata[$k]['tavalue'] = explode(',', $v['tavalue']);
			
			if ($v['taclass']==0){
				$selvalue = '';
				foreach($gavalue as $kg=>$vg){
					if ($vg['type_attr_taid'] == $v['taid']){
						$selvalue = $vg['gavalue'];
						break;
					}
				}
				$cateTypeAttrdata[$k]['selvalue'] = $selvalue;
				$attrs[$v['taid']] = $cateTypeAttrdata[$k];
			}else{
				foreach($gavalue as $kg=>$vg){
					if ($vg['type_attr_taid'] == $v['taid']){
						$cateTypeAttrdata[$k]['selvalue'][] = $vg;
					}
				}
				$specs[$v['taid']] = $cateTypeAttrdata[$k];
			}
			
		}
		$this->assign('oldAttrs', $attrs);
		$this->assign('oldSpecs', $specs);
//		p($attrs);
//		p($specs);
		
		$this->assign('brandData', K('Brand')->all());
	    $this->display();
	}
	
	// 删除商品
	public function del(){
		$gcid = Q('get.gcid', 0, 'intval');
	    $gid = Q('get.gid', 0, 'intval');
		if (!$this->model->del($gid)){
			$this->error($this->model->error);
		}
		$this->success("删除商品成功", U('Index', array('gcid'=>$gcid)));
	}
	
	// 异步通过cid获取类型属性
	public function ajaxGetTypeAttr(){
	    if (!IS_AJAX) return;
		$cid = Q('post.cid', 0, 'intval');
		$data = M()->join("__category__ AS a JOIN __type_attr__ AS ta ON a.tid=ta.tid")->where("cid={$cid}")->all();
		foreach($data as $k=>$v){
			$data[$k]['tavalue'] = explode(',', $v['tavalue']);
		}		
		$this->ajax($data);
	}
	
	// 上传
	public function ajaxUpload()
	{
	   $upload = new Upload('Upload/Content/' . date('y/m'));
	   $file = $upload->upload();
	   if (empty($file)) {
	       $this->ajax('上传失败');
	   } else {
	       $data = $file[0];
	       $this->ajax($data);
	   }
	}
 }
 
 
 
 
 
 
 
 
 
 
 


 ?>